<?php

namespace App\Http\Controllers\Admin;

use App\Models\Menu;

/**
 * 后台配置控制器
 */
class MenuController extends BaseController
{


    public function __construct(Menu $menu)
    {
        parent::__construct();
        $this->menu = $menu;
    }

    /**
     * 后台菜单首页
     */
    public function getIndex()
    {
        $res = $this->menu->getMenuList();
        return view('admin.menu.index', compact('res'));
    }

    /**
     * 新增菜单
     */
    public function getAdd()
    {
        $menu = $this->menu->getMenuTree();
        $info['pid'] = request('pid');
        return view('admin.menu.edit', compact('menu', 'info'));
    }

    public function postAdd()
    {
        $res = $this->menu->addOrUpdate();
        if (!$res) {
            return $this->error($this->menu->getError() ? $this->menu->getError() : '新增失败');
        }
        return $this->success('新增成功', urlAdmin('menu', 'getIndex', ['pid' => request('pid')]));
    }

    /**
     * 编辑配置
     */
    public function getEdit()
    {
        $id = request('id');
        $info = $this->menu->_getOneById($id);
        $menu = $this->menu->getMenuTree();
        return view('admin.menu.edit', compact('menu', 'info'));
    }

    public function postEdit()
    {
        $res = $this->menu->addOrUpdate();
        if (!$res) {
            return $this->error($this->menu->getError() ? $this->menu->getError() : '更新失败');
        }
        return $this->success('更新成功', urlAdmin('menu', 'getIndex', ['pid' => request('pid')]));
    }

    /**
     * 删除后台菜单
     */
    public function anyDel()
    {
        $res = $this->menu->del();
        if (!$res) {
            return $this->error($this->menu->getError());
        }
        return $this->success($res);
    }

    public function getToogleHide()
    {
        $data['hide'] = request('value');
        $where['id'] = request('id');
        $msg = ['success' => '状态隐藏成功！', 'error' => '状态隐藏失败！'];
        return $this->editRow($this->menu, $data, $where, $msg);
    }

//    public function toogleDev($id, $value = 1)
//    {
//        return $this->editRow($this->menu, ['is_dev' => $value], ['id' => $id]);
//    }

    public function importFile()
    {
        $res = $this->menu->importFile();
        if (!$res) {
            return $this->error($this->menu->getError());
        }
        return $this->success($res);
    }

    public function getImport()
    {
        $pid = intval(request('pid'));
        $data = $this->menu->_getOneById($pid);
        return view('admin.menu.import', compact('data', 'pid'));
    }

    public function postImport()
    {
        $res = $this->menu->import();
        if (!$res) {
            return $this->error($this->menu->getError());
        }
        $this->success($res, urlAdmin('menu', 'getIndex', ['pid' => request('id')]));
    }

    /**
     * 菜单排序
     */
    public function getSort()
    {
        $where['status'] = ['>', -1];
        if (request()->has('ids')) {
            $where['id'] = ['in', [request('ids')]];
        }
        if (request()->has('pid')) {
            $where['pid'] = request('pid');
        }
        $rows = $this->menu->_getList($where, 'id,title', ['sort' => 'asc', 'id' => 'asc'])->toArray();
        return view('admin.menu.sort', compact('rows'));
    }

    public function postSort()
    {
        $ids = request('ids');
        $ids = explode(',', $ids);
        $data = [];
        foreach ($ids as $key => $value) {
            $data[] = ['id' => $value, 'sort' => $key + 1];
        }
        $res = $this->menu->_updateBatch($data);
        $this->menu->generate();
        if (!$res) {
            return $this->error('排序失败！');
        }
        return $this->success('排序成功！');
    }
}